<?php
class common_media_concept extends system_concept_abstract {
	
	public function init() {	
		$this->setModel('common_media_model', 'media');
		$this->setModelCols('media', array('md5', 'size', 'type', 'htype', 'extension', 'creationtime'));
	}

	public function save($row) {
		if(!$row->getMd5()) {
			throw new common_exception("Md5 can not be empty.");
		}
		if(!preg_match('/^[0-9a-z]{32}$/is', $row->getMd5())) {
			throw new common_exception("Md5 Hash formation error.");
		}
		$row->setSize(intval($row->get('size')));
		$row->setHtype(intval($row->get('htype')));
		if($row->getType() == null) {
			throw new common_exception("Type can not be empty.");
		}
		$row->setType(trim($row->get('type')));
		$row->setExtension(trim($row->get('extension')));	
		
		if($row->getMd5()) {
			if($this->fetch(array('md5 = ?' => $row->getMd5()))) {
				$this->getModel('media')->update($row->getModelData('media'), array('md5=?' => $row->getMd5()));
			} else {
				$row->setCreationtime(system_date::timestamp());
				$this->getModel('media')->insert($row->getModelData('media'));
			}
			return $row;
		}
	}
	
	public function delete($row) {
		return ;
	}

}